using System.Collections.Generic;
using TestCases.CompareAssert.Core.Compositions.Interfaces;
using TestCases.CompareAssert.Core.Interfaces;

namespace TestCases.CompareAssert.Core.Compositions
{
    public class SingleStrategy<TItem, TResult>: ICompositionStrategy<TItem, TResult> where TResult: IResultProvider
    {

        public IResultBuilder Execute(ICompositionComparer<TItem, TResult> comparer, IEnumerable<TItem> items, ICompareContext context)
        {
            var resultItems = new List<TResult>();
            foreach (var item in items)
            {
                var resultItem = comparer.CompareBy(item, context);
                resultItems.Add(resultItem);
                
                if (resultItem.GetStatus() != CompareStatus.NotApplicable)
                {
                    return comparer.ComposeItemResults(resultItem.GetStatus(), context, resultItems);
                }
            }
            return comparer.ComposeItemResults(CompareStatus.NotApplicable, context, resultItems);
        }
    }
}